﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HostingDomainWebsite;
using Obout.Grid;
using Hosting.Class;
using Obout.SuperForm;
using System.Web.UI.HtmlControls;

namespace Hosting
{
    public partial class ServicePackage : System.Web.UI.Page
    {
        protected SiteMaster master;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["username"] == null)
            {
                Response.Redirect("AdminLogin.aspx");
            }

            master = Page.Master as SiteMaster;
            master.setCurrentNav("ServiceManagement", "Services");
            sHostingDomain.Attributes["onchange"] = "filterGrid(this)";
        }

        #region [LinqDataSource Category Detail Selecting]
        protected void ldsHCDetail_Selecting(object sender, LinqDataSourceSelectEventArgs e)
        {
            CategoryDetail cDetail = new CategoryDetail();
            var result = cDetail.getAll();
            e.Result = result;
        }
        #endregion

        #region [Init tab]
        protected void initTab()
        {
            master.changeNameTab1("Hosting");
        }
        #endregion

        #region [Insert hosting]
        /*Return:
         * 1    : successfully
         * 10   : name is null or empty
         * 11   : name is greater than 50 characters
         * 20   : currency is null or empty
         * 21   : currency is greater than 50 characters
         * 30   : type is null or empty
         * 31   : type is greater than 50 characters
         * 40   : description is greater than 2000 characters
         */
        protected void gHosting_InsertCommand(object sender, GridRecordEventArgs e)
        {
            Hosting.Class.ServicePackage package = new Class.ServicePackage();
            var isValid = package.addValid(e.Record["SPName"].ToString(), e.Record["SPDescription"].ToString()
                , e.Record["SPCurrency"].ToString(), e.Record["SPType"].ToString());

            if (isValid == 1)
            { 
                var isInsert = package.add(e.Record["SPName"].ToString(), e.Record["SPDescription"].ToString()
                , e.Record["SPCurrency"].ToString(), e.Record["SPType"].ToString(), Boolean.Parse(e.Record["SPActive"].ToString()));
                if (isInsert == 0)
                {
                    master.getMessageSuccess("Insert successfully!");
                }
                else
                {
                    master.getMessageError("There is an error when insert!");
                }
            }
            else if(isValid == 10)
            {
                master.getMessageAttention("name is null or empty");
            }
            else if (isValid == 11)
            {
                master.getMessageAttention("name is greater than 50 characters");
            }
            else if (isValid == 20)
            {
                master.getMessageAttention("currency is null or empty");
            }
            else if (isValid == 21)
            {
                master.getMessageAttention("currency is greater than 50 characters");
            }
            else if (isValid == 30)
            {
                master.getMessageAttention("type is null or empty");
            }
            else if (isValid == 31)
            {
                master.getMessageAttention("type is greater than 50 characters");
            }
            else if (isValid == 40)
            {
                master.getMessageAttention("description is greater than 2000 characters");
            }
        }
        #endregion

        #region [Update hosting]
        /*Return:
         * 1    : successfully
         * 2    : id is null or empty
         * 3    : id is not valid
         * 10   : name is null or empty
         * 11   : name is greater than 50 characters
         * 20   : currency is null or empty
         * 21   : currency is greater than 50 characters
         * 30   : type is null or empty
         * 31   : type is greater than 50 characters
         * 40   : description is greater than 2000 characters
         */
        protected void gHosting_UpdateCommand(object sender, GridRecordEventArgs e)
        {
            Hosting.Class.ServicePackage package = new Class.ServicePackage();
            var isValid = package.editValid(e.Record["SPId"].ToString(), e.Record["SPName"].ToString(), e.Record["SPDescription"].ToString()
                , e.Record["SPCurrency"].ToString(), e.Record["SPType"].ToString());

            if (isValid == 1)
            {
                var isUpdate = package.edit(e.Record["SPId"].ToString(), e.Record["SPName"].ToString(), e.Record["SPDescription"].ToString()
                , e.Record["SPCurrency"].ToString(), e.Record["SPType"].ToString(), Boolean.Parse(e.Record["SPActive"].ToString()));
                if (isUpdate == 0)
                {
                    master.getMessageSuccess("Update successfully!");
                }
                else
                {
                    master.getMessageError("There is an error when Update!");
                }
            }
            else if (isValid == 2)
            {
                master.getMessageAttention("id is null or empty");
            }
            else if (isValid == 3)
            {
                master.getMessageAttention("id is not valid");
            }
            else if (isValid == 10)
            {
                master.getMessageAttention("name is null or empty");
            }
            else if (isValid == 11)
            {
                master.getMessageAttention("name is greater than 50 characters");
            }
            else if (isValid == 20)
            {
                master.getMessageAttention("currency is null or empty");
            }
            else if (isValid == 21)
            {
                master.getMessageAttention("currency is greater than 50 characters");
            }
            else if (isValid == 30)
            {
                master.getMessageAttention("type is null or empty");
            }
            else if (isValid == 31)
            {
                master.getMessageAttention("type is greater than 50 characters");
            }
            else if (isValid == 40)
            {
                master.getMessageAttention("description is greater than 2000 characters");
            }
        }
        #endregion

        #region [Delete hosting]
        protected void gHosting_DeleteCommand(object sender, GridRecordEventArgs e)
        {
            Hosting.Class.ServicePackage service = new Class.ServicePackage();
            var isDelete = service.delete(e.Record["SPId"].ToString());
            if (isDelete != 0)
            {
                master.getMessageError("There is an error when delete an Service Package!");
            }
        }
        #endregion

        #region [Insert Cate]
        /*Return:
         * 1    : successfully
         * 4    : Package id is null or empty
         * 5    : Package id is not valid
         * 20   : Fee is not valid
         * 40   : description is greater than 2000 characters
         */
        protected void dgCate_InsertCommand(object sender, GridRecordEventArgs e)
        {
            Hosting.Class.ServiceCategories cate = new Hosting.Class.ServiceCategories();
            var isValid = cate.addValid(e.Record["CName"].ToString(), e.Record["CDescription"].ToString(), e.Record["CFee"].ToString(), e.Record["SPId"].ToString());
            if (isValid == 1)
            {
                var isInsert = cate.add(e.Record["CName"].ToString(), e.Record["CDescription"].ToString(), e.Record["CFee"].ToString()
                    , e.Record["SPId"].ToString(), Boolean.Parse(e.Record["CActive"].ToString()));
                if (isInsert == 0)
                {
                    master.getMessageSuccess("Insert successfully!");
                }
                else
                {
                    master.getMessageError("There is an error when insert Category!");
                }
            }
            else if(isValid == 4)
            {
                master.getMessageAttention("Package id is null or empty");
            }
            else if (isValid == 5)
            {
                master.getMessageAttention("Package id is not valid");
            }
            else if (isValid == 20)
            {
                master.getMessageAttention("Fee is not valid");
            }
            else if (isValid == 40)
            {
                master.getMessageAttention("description is greater than 2000 characters");
            }
        }
        #endregion

        #region [Update Cate]
        protected void dgCate_UpdateCommand(object sender, GridRecordEventArgs e)
        {
            Hosting.Class.ServiceCategories cate = new Hosting.Class.ServiceCategories();
            var isValid = cate.editValid(e.Record["CId"].ToString(),e.Record["CName"].ToString(), e.Record["CDescription"].ToString()
                , e.Record["CFee"].ToString(), e.Record["SPId"].ToString());
            if (isValid == 1)
            {
                var isEdit = cate.edit(e.Record["CId"].ToString(), e.Record["CName"].ToString(), e.Record["CDescription"].ToString(), e.Record["CFee"].ToString()
                    , e.Record["SPId"].ToString(), Boolean.Parse(e.Record["CActive"].ToString()));
                if (isEdit == 0)
                {
                    master.getMessageSuccess("Update successfully!");
                }
                else
                {
                    master.getMessageError("There is an error when update Category!");
                }
            }
            else if (isValid == 1)
            {
                master.getMessageAttention("id is null or empty");
            }
            else if (isValid == 2)
            {
                master.getMessageAttention("id is not valid");
            }
            else if (isValid == 4)
            {
                master.getMessageAttention("Package id is null or empty");
            }
            else if (isValid == 5)
            {
                master.getMessageAttention("Package id is not valid");
            }
            else if (isValid == 20)
            {
                master.getMessageAttention("Fee is not valid");
            }
            else if (isValid == 40)
            {
                master.getMessageAttention("description is greater than 2000 characters");
            }
        }
        #endregion

        #region [Delete Cate]
        protected void dgCate_DeleteCommand(object sender, GridRecordEventArgs e)
        {
            ServiceCategories cate = new ServiceCategories();
            var isDelete = cate.delete(e.Record["CId"].ToString());
            if (isDelete != 0)
            {
                master.getMessageError("There is an error when delete a Service Category!");
            }
        }
        #endregion

        #region [Insert Category Detail]
        /*Return:
         * 1    : successfully
         * 2    : Category ID is null or empty
         * 3    : Category ID is not valid
         * 4    : Category Title ID is null or epmty
         * 5    : Category Title is not valid
         * 6    : Content is greater than 2000 characters
         * 7    : Category ID and Category Title ID is exists in Category Detail
         */
        protected void dgCDetail_InsertCommand(object sender, GridRecordEventArgs e)
        {
            CategoryDetail detail = new CategoryDetail();
            var isValid = detail.addValid(e.Record["CId"].ToString(), e.Record["CTId"].ToString(), e.Record["CDContent"].ToString());
            if (isValid == 1)
            {
                var isInsert = detail.add(e.Record["CId"].ToString(), e.Record["CTId"].ToString()
                    , e.Record["CDContent"].ToString(), Boolean.Parse(e.Record["CDActive"].ToString()));
                if (isInsert == 0)
                {
                    master.getMessageSuccess("Insert successfully!");
                }
                else
                {
                    master.getMessageError("There is an error when insert category detail!");
                }
            }
            else if(isValid == 2)
            {
                master.getMessageAttention("Category ID is null or empty");
            }
            else if (isValid == 3)
            {
                master.getMessageAttention("Category ID is not valid");
            }
            else if (isValid == 4)
            {
                master.getMessageAttention("Category Title ID is null or epmty");
            }
            else if (isValid == 5)
            {
                master.getMessageAttention("Category Title is not valid");
            }
            else if (isValid == 6)
            {
                master.getMessageAttention("Content is greater than 2000 characters");
            }
            else if (isValid == 7)
            {
                master.getMessageAttention("Category ID and Category Title ID is exists in Category Detail");
            }
        }
        #endregion

        #region [Update Category Detail]
        /*Return:
         * 1    : successfully
         * 2    : Category ID is null or empty
         * 3    : Category ID is not valid
         * 4    : Category Title ID is null or epmty
         * 5    : Category Title is not valid
         * 6    : Content is greater than 2000 characters
         */
        protected void dgCDetail_UpdateCommand(object sender, GridRecordEventArgs e)
        {
            CategoryDetail detail = new CategoryDetail();
            var isValid = detail.editValid(e.Record["CId"].ToString(), e.Record["CTId"].ToString(), e.Record["CDContent"].ToString());
            if (isValid == 1)
            {

                var isInsert = detail.edit(oldCId.Value, oldCTId.Value, e.Record["CId"].ToString(), e.Record["CTId"].ToString()
                    , e.Record["CDContent"].ToString(), Boolean.Parse(e.Record["CDActive"].ToString()));
                if (isInsert == 0)
                {
                    master.getMessageSuccess("Update successfully!");
                }
                else
                {
                    master.getMessageError("There is an error when update category detail!");
                }
            }
            else if (isValid == 2)
            {
                master.getMessageAttention("Category ID is null or empty");
            }
            else if (isValid == 3)
            {
                master.getMessageAttention("Category ID is not valid");
            }
            else if (isValid == 4)
            {
                master.getMessageAttention("Category Title ID is null or epmty");
            }
            else if (isValid == 5)
            {
                master.getMessageAttention("Category Title is not valid");
            }
            else if (isValid == 6)
            {
                master.getMessageAttention("Content is greater than 2000 characters");
            }
        }
        #endregion

        #region [Delete Category Detail]
        protected void dgCDetail_DeleteCommand(object sender, GridRecordEventArgs e)
        {
            CategoryDetail detail = new CategoryDetail();
            var isDelete = detail.delete(e.Record["CId"].ToString(), e.Record["CTId"].ToString());
            if (isDelete != 0)
            {
                master.getMessageError("There is an error when delete a Category Detail!");
            }
        }
        #endregion
    }
}